use derive_builder::Builder;
use serde::{Deserialize, Serialize};

use crate::{error::Error, modeling::Value};

#[derive(Debug, Serialize, Default, Deserialize, Clone, PartialEq)]
pub enum WorkflowFileTransferMethod {
    #[default]
    #[serde(rename = "local_file")]
    LocalFile,
}

#[derive(Debug, Serialize, Default, Deserialize, Clone, PartialEq)]
pub enum WorkflowFileType {
    #[default]
    #[serde(rename = "document")]
    Document,
}

#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)]
#[builder(name = "WorkflowFileVariableBuilder")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "Error"))]
pub struct WorkflowFileVariable {
    pub transfer_method: WorkflowFileTransferMethod,
    pub r#type: WorkflowFileType,
    pub upload_file_id: String,
    pub url: Option<String>,
}

impl TryFrom<Value> for WorkflowFileVariable {
    type Error = anyhow::Error;

    fn try_from(value: Value) -> Result<Self, Self::Error> {
        let v: serde_json::Value = value.try_into()?;
        let res = serde_json::from_value(v).map_err(|err| {
            tracing::debug!("{}", err);
            anyhow::anyhow!("{}", err)
        })?;
        Ok(res)
    }
}
